<?php defined('BASE_PATH') or die('No direct script access.');
/**
 * Gialac php framework
 * Template Library
 * @package    systemp
 * @category   Template
 * @author     Gialachoanglong
 * @copyright  (c) 2011
 */
Class Template {

    private $registry;
    private $vars = array();
    function __construct($registry) {
        $this->registry = $registry;
    }

    public function __set($index, $value) {
        $this->vars[$index] = $value;
    }

    function write($region, $content, $overwrite = FALSE) {
        if ($overwrite === TRUE) { // Should we append the content or overwrite it
            $this->vars[$region]['content'] = array($content);
        }
        else {
            $this->vars[$region] = $content;
        }
    }

    function view($region,$module, $name, $data=NULL) {
        $path = MODULE . $module . '/views' . '/' . $name . '.php';
        if (file_exists($path) == false) {
            $path = BASE_PATH . '/application/views/' . $name . '.php';
            if (file_exists($path) == false) {
                echo 'Template not found in : ' . $path;
                return false;
            }
        }
        // Nạp giá trị biến được gán vào template
        $this->vars[$region][] = array('path' => $path, 'data' => $data);
    }

    function render() {
        foreach ($this->vars as $key => $value) {
            $$key = $value;
        }
        include $this->vars['template'];
    }

}

?>
